package com.gowants.pems.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gowants.pems.aspect.Importance;
import com.gowants.pems.aspect.OperType;
import com.gowants.pems.aspect.ScmsAopLog;
import com.gowants.pems.config.AccessLimit;
import com.gowants.pems.controller.BaseController;
import com.gowants.pems.model.ErrorCode;
import com.gowants.pems.service.ErrorCodeService;
import com.gowants.pems.util.JSONList;
import com.gowants.pems.util.JSONMap;

@RestController
@RequestMapping(value = "/ErrorCode")
public class ErrorCodeController extends BaseController {
	@Autowired
	private ErrorCodeService errorCodeService;

	/**
	 * 列表查询
	 * 
	 * @param {"page":"1","limit":"10","reqData":{"condition":[{"field":"code","keyword":""},{"field":"status_code","keyword":""},{"field":"channel","keyword":""},{"field":"value_zh","keyword":""},
	 *        {"field":"icon","keyword":""},{"field":"descript","keyword":""}]}}
	 * @param
	 * @remake
	 */
	@RequestMapping(value = "/getList", method = RequestMethod.POST)
	@AccessLimit(seconds = 1, maxCount = 3,needLogin=true) //1秒内 允许请求3次
	@ScmsAopLog(description = "错误码查询", opType = OperType.execute, importance = Importance.commonly)
	public JSONMap getCardList(@RequestParam(defaultValue = "1", required = false, value = "page") int page,
			@RequestParam(defaultValue = "10", required = false, value = "limit") int limit,
			@RequestParam(defaultValue = "", required = false, value = "reqData") String reqData,
			HttpServletRequest req) {
		try {
			if (!isPower("/ErrorCode/getList.do")) {
				return doResponse(req, "TSMS_0000228");
			}
			JSONMap resultMap = new JSONMap();
			ErrorCode record = new ErrorCode();
			// 查询条件
			if (!"".equals(reqData)) {
				JSONList jsonConditon = getCondition(reqData);
				for (Object con : jsonConditon) {
					this.buildCondition(con, record);
				}
			}

			PageHelper.startPage(page, limit);
			List<ErrorCode> ErrorCodeList = errorCodeService.select(record);
			PageInfo<ErrorCode> pageInfo = new PageInfo<ErrorCode>(ErrorCodeList);
			pageSetData(req, resultMap, page, limit, pageInfo.getPages(), "", ErrorCodeList, pageInfo.getTotal());
			return doResponse(req, "", resultMap);

		} catch (Exception e) {
			return doResponse(req, e.getMessage());
		}

	}

	/**
	 * <构建查询条件>
	 * 
	 * @param obj
	 * @param record
	 * @remake
	 */
	private void buildCondition(Object obj, ErrorCode record) {
		JSONMap condition = JSONMap.parseJSONMap(obj.toString());
		String field = condition.getString("field");
		String keyword = condition.getString("keyword");

		if (StringUtils.isNotEmpty(field) && StringUtils.isNotEmpty(keyword)) {

			switch (field) {
			case "errorCode":
				record.setErrorCode(keyword);
				break;
			case "statusCode":
				record.setStatusCode(Integer.valueOf(keyword));
				break;
			case "channel":
				record.setChannel(keyword);
				break;
			case "valueZh":
				record.setValueZh(keyword);
				break;
			case "icon":
				record.setIcon(Integer.valueOf(keyword));
				break;
			case "descript":
				record.setDescript(keyword);
				break;
			}
		}
	}

	/**
	 * <保存数据>
	 * 
	 * @param request
	 * @param req     {"statusCode": "1","channel": "1","valueZh": "1","icon":
	 *                "1","descript": "1"}
	 * @return
	 * @remake
	 */
	@RequestMapping(value = "/save")
	public JSONMap save(String request, HttpServletRequest req) {
		if (!isPower("/ErrorCode/save.do")) {
			return doResponse(req, "TSMS_0000228");
		}
		String code = "TSMS_0000036";// 添加失败
		if (StringUtils.isBlank(request)) {
			code = "TSMS_0000144";// 请传入参数
			return doResponse(req, code, null);
		}

		String statusCode = getRequestBody(request).getString("statusCode");
		if (StringUtils.isBlank(statusCode)) {
			code = "TSMS_0000146";// 请输入状态码
			return doResponse(req, code, null);
		}

		String channel = getRequestBody(request).getString("channel");
		if (StringUtils.isBlank(channel)) {
			code = "TSMS_0000147";// 请选择渠道类型
			return doResponse(req, code, null);
		}
		String valueZh = getRequestBody(request).getString("valueZh");
		if (StringUtils.isBlank(valueZh)) {
			code = "TSMS_0000148";// 请输入提示信息
			return doResponse(req, code, null);
		}

		String icon = getRequestBody(request).getString("icon");
		String descript = getRequestBody(request).getString("descript");
		String errorCode = getRequestBody(request).getString("errorCode");

		ErrorCode record = new ErrorCode();
		record.setChannel(channel);
		record.setDescript(descript);
		if (!StringUtils.isBlank(icon)) {
			record.setIcon(Integer.valueOf(icon));
		}
		record.setStatusCode(Integer.valueOf(statusCode));
		record.setValueZh(valueZh);
		// 生成错误码
//	 String errorCode="TSMS_0000001";
//	 ErrorCode e=errorCodeService.getTopOneByCode();
//	 if(e!=null)
//	 {
//		 String errorCodeold=e.getErrorCode();
//		 
//		String[] str= errorCodeold.split("_");
//		if(str.length>1)
//		{
//		  int strs=Integer.valueOf(str[1]);
//		  strs= strs+1;
//		  if(Integer.toString(strs).length()==1)
//		  {
//			  errorCode="TSMS_000000"+ strs;
//		  }
//		  else
//			  if(Integer.toString(strs).length()==2)
//			  {
//				  errorCode="TSMS_00000"+ strs;
//			  } 
//			  else
//				  if(Integer.toString(strs).length()==3)
//				  {
//					  errorCode="TSMS_0000"+ strs;
//				  }  
//				  else
//					  if(Integer.toString(strs).length()==4)
//					  {
//						  errorCode="TSMS_000"+ strs;
//					  }  
//					  else
//						  if(Integer.toString(strs).length()==5)
//						  {
//							  errorCode="TSMS_00"+ strs;
//						  }  
//						  else
//							  if(Integer.toString(strs).length()==6)
//							  {
//								  errorCode="TSMS_0"+ strs;
//							  } 
//							  else
//								  if(Integer.toString(strs).length()==7)
//								  {
//									  errorCode="TSMS_"+ strs;
//								  }  
//		  
//		  
//		
//		}
//		 
//	 }
//	 
		record.setErrorCode(errorCode);

		int executeNum = errorCodeService.save(record);
		if (executeNum > 0) {
			code = isClient(req) ? "" : "TSMS_0000001";// 添加成功
		}

		return doResponse(req, code, null);

	}

	/**
	 * <保存数据>
	 * 
	 * @param request
	 * @param req     {"id": "152","statusCode_edit": "400","channel_edit":
	 *                "ALL","valueZh_edit": "测试11","icon_edit": "5","descript":
	 *                "测试数据"}
	 * @return
	 * @remake
	 */
	@RequestMapping(value = "/update")
	public JSONMap update(String request, HttpServletRequest req) {
		if (!isPower("/ErrorCode/update.do")) {
			return doResponse(req, "TSMS_0000228");
		}
		ErrorCode record = new ErrorCode();
		String code = "TSMS_0000053";// 修改失败
		if (StringUtils.isBlank(request)) {
			code = "TSMS_0000144";// 请传入参数
			return doResponse(req, code, null);
		}
		String id = getRequestBody(request).getString("id");
		if (id != null && !"".equals(id)) {
			record.setId(Integer.valueOf(id));
		} else {
			code = "TSMS_0000142";// 请输入字典id
			return doResponse(req, code, null);
		}
		String statusCode = getRequestBody(request).getString("statusCode_edit");
		String channel = getRequestBody(request).getString("channel_edit");
		String valueZh = getRequestBody(request).getString("valueZh_edit");
		String icon = getRequestBody(request).getString("icon_edit");
		String descript = getRequestBody(request).getString("descript");
		String errorCode = getRequestBody(request).getString("errorCode_edit");
		record.setErrorCode(errorCode);
		record.setChannel(channel);
		record.setDescript(descript);

		if (StringUtils.isBlank(icon)) {
			record.setIcon(null);
		} else {
			record.setIcon(Integer.valueOf(icon));
		}

		if (!StringUtils.isBlank(statusCode)) {
			record.setStatusCode(Integer.valueOf(statusCode));
		}
		record.setValueZh(valueZh);
		int executeNum = errorCodeService.update(record);
		if (executeNum > 0) {
			code = isClient(req) ? "" : "TSMS_0000002";// 修改成功
		}

		return doResponse(req, code, null);

	}

	/**
	 * <保存数据>
	 * 
	 * @param request
	 * @param req     {"id": "152"}
	 * @return
	 * @remake
	 */
	@RequestMapping(value = "/delete")
	public JSONMap delete(String request, HttpServletRequest req) {
		if (!isPower("/ErrorCode/delete.do")) {
			return doResponse(req, "TSMS_0000228");
		}
		String code = "TSMS_0000048";// 删除失败
		if (StringUtils.isBlank(request)) {
			code = "TSMS_0000144";// 请传入参数
			return doResponse(req, code, null);
		}
		String id = getRequestBody(request).getString("id");
		if (id != null && !"".equals(id)) {

			int executeNum = errorCodeService.delete(id);
			if (executeNum > 0) {
				code = isClient(req) ? "" : "TSMS_0000050";// 删除成功
			}

		} else {
			code = "TSMS_0000142";// 请输入字典id
			return doResponse(req, code, null);
		}
		return doResponse(req, code, null);
	}

}
